package com.jfinal.render;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

import com.jfinal.log.Log;
import com.jfinal.render.Render;

public class XlsTableRender extends Render {

	protected final Log LOG = Log.getLog(getClass());
	private final static String CONTENT_TYPE = "application/msexcel;charset=" + getEncoding();
	private String table;
	private String fileName = "file1.xls";
	
	public XlsTableRender table(String table) {
        this.table = table;
		return this;
    }

	public XlsTableRender fileName(String fileName) {
		this.fileName = fileName;
		return this;
	}
	
	@Override
	public void render() {
		response.reset();
		response.setHeader("Content-disposition","attachment; filename="+fileName);
		response.setContentType(CONTENT_TYPE);
		try {
			PrintWriter out = response.getWriter();
			out.println("<body>");
			out.println(new String(table.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
			out.println("</body>");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
